package qianfeng.com.fan_shu_xiao_bao.view.Information;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import qianfeng.com.fan_shu_xiao_bao.R;
import qianfeng.com.fan_shu_xiao_bao.view.Information.commend.InformationCommendFragment;
import qianfeng.com.fan_shu_xiao_bao.view.Information.gif.GIFFragment;
import qianfeng.com.fan_shu_xiao_bao.view.Information.ordinary.InformationOrdinaryFragment;
import qianfeng.com.fan_shu_xiao_bao.view.Information.subscribe.InformationSubscribeFragment;
import qianfeng.com.fan_shu_xiao_bao.view.Information.welfare.WelfareFragment;
import qianfeng.com.fan_shu_xiao_bao.view.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 * 资讯的Fragment
 */
public class InformationFragment extends BaseFragment {

    @InjectView(R.id.fragment_information_TabLayout)
    TabLayout tabLayout;
    @InjectView(R.id.fragment_information_ViewPager)
    ViewPager viewPager;
    private List<String> titles;
    private BaseFragment[] fragments;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_information, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initData();

        viewPager.setAdapter(new Information_ViewPager_Adapter(getChildFragmentManager(), fragments, titles));
        tabLayout.setupWithViewPager(viewPager);
    }

    //初始化此页数据
    private void initData() {
        this.titles = new ArrayList<String>();
        titles.add("推荐");
        titles.add("订阅");
        titles.add("热门");
        titles.add("动漫");
        titles.add("游戏");
        titles.add("娱乐");
        titles.add("福利");
        titles.add("奇葩");
        titles.add("GIF");
        titles.add("搞笑");

        fragments = new BaseFragment[titles.size()];
        fragments[titles.indexOf("推荐")] = new InformationCommendFragment();
        fragments[titles.indexOf("订阅")] = new InformationSubscribeFragment();
        fragments[titles.indexOf("福利")] = new WelfareFragment();
        fragments[titles.indexOf("GIF")] = new GIFFragment();
        for (int i = 0; i < fragments.length; i++) {
            if (fragments[i] == null) {
                fragments[i] = new InformationOrdinaryFragment();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    /**
     * Created by Li_ke on 2017/1/17.
     */
    public static class Information_ViewPager_Adapter extends FragmentPagerAdapter {
        private final BaseFragment[] fragments;
        private final List<String> titles;

        public Information_ViewPager_Adapter(FragmentManager fm, BaseFragment[] fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
